﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYDuctConnectAirrTerminalByRectangle : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			this.uiApp = commandData.Application;
			UIDocument activeUIDocument = this.uiApp.ActiveUIDocument;
			this.doc = activeUIDocument.Document;
			this.sel = activeUIDocument.Selection;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2,
				(ViewType)4,
				(ViewType)3
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			RevitVersionFuncs.ClearSelection(this.sel);
			if (HYDuctConnectAirrTerminalByRectangle.hWndRevit == null)
			{
				HYDuctConnectAirrTerminalByRectangle.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			BatchConnTerminalForm batchConnTerminalForm = new BatchConnTerminalForm();
			batchConnTerminalForm.Show(HYDuctConnectAirrTerminalByRectangle.hWndRevit);
			int wMsg = 7;
			HYDuctConnectAirrTerminalByRectangle.SendMessage(HYDuctConnectAirrTerminalByRectangle.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			new HYDuctConnect();
			string text = "风管连接风口";
			RectangleConnTerminalCheck rectangleConnTerminalCheck = new RectangleConnTerminalCheck();
			Duct duct = null;
			List<FamilyInstance> list = null;
			Result result;
			for (;;)
			{
				Transaction transaction = new Transaction(this.doc, text);
				try
				{
					transaction.Start();
					XYZ globalPoint = null;
					HYDuctConnectUtil hyductConnectUtil = new HYDuctConnectUtil();
					switch (batchConnTerminalForm.BatchConnStyle)
					{
					case BatchConnTerminalForm.ConnStyle.Fitting:
					{
						batchConnTerminalForm.groupBox1.Enabled = true;
						batchConnTerminalForm.hyGroupBoxBlackSide2.Enabled = true;
						string text2 = rectangleConnTerminalCheck.SelectBatchConnElementSet(this.sel, ref duct, ref list, ref globalPoint, false);
						if (!string.IsNullOrEmpty(text2))
						{
							YJKMessageBox.Information(text2);
						}
						else
						{
							int ductConnectType = rectangleConnTerminalCheck.GetDuctConnectType(duct);
							int terminalConnType = batchConnTerminalForm.TerminalConnType;
							int alignType = batchConnTerminalForm.AlignType;
							int num = hyductConnectUtil.BatchConnTerminalByFitting(duct, list, globalPoint, ductConnectType, terminalConnType, alignType);
							int num2 = list.Count - num;
							if (num2 > 0)
							{
								YJKMessageBox.Information(string.Format("存在{0}个风口不符合管件连接条件，请手动调整或删除。", num2));
							}
						}
						break;
					}
					case BatchConnTerminalForm.ConnStyle.OnDuct:
					{
						batchConnTerminalForm.groupBox1.Enabled = false;
						batchConnTerminalForm.hyGroupBoxBlackSide2.Enabled = false;
						List<Duct> listDuct = new List<Duct>();
						string text2 = rectangleConnTerminalCheck.SelectBatchConnElementSet(this.sel, ref listDuct, ref list);
						if (!string.IsNullOrEmpty(text2))
						{
							YJKMessageBox.Information(text2);
						}
						else
						{
							List<FamilyInstance> list2 = new List<FamilyInstance>();
							foreach (KeyValuePair<Duct, List<FamilyInstance>> keyValuePair in rectangleConnTerminalCheck.GroupDuctsAndTerminals(listDuct, list, ref list2, true))
							{
								rectangleConnTerminalCheck.BatchConnCloseTerminal(keyValuePair.Key, keyValuePair.Value);
							}
							if (list2 != null && list2.Count > 0)
							{
								text2 = string.Format("存在{0}个风口不符合贴管连接条件，请手动调整或删除。", list2.Count);
								YJKMessageBox.Information(text2);
							}
						}
						break;
					}
					case BatchConnTerminalForm.ConnStyle.Intubation:
					{
						batchConnTerminalForm.groupBox1.Enabled = true;
						batchConnTerminalForm.hyGroupBoxBlackSide2.Enabled = true;
						List<Duct> listDuct2 = new List<Duct>();
						string text2 = rectangleConnTerminalCheck.SelectBatchConnElementSet(this.sel, ref listDuct2, ref list);
						if (!string.IsNullOrEmpty(text2))
						{
							YJKMessageBox.Information(text2);
						}
						else
						{
							int terminalConnType2 = batchConnTerminalForm.TerminalConnType;
							int alignType2 = batchConnTerminalForm.AlignType;
							List<FamilyInstance> list3 = new List<FamilyInstance>();
							Dictionary<Duct, List<FamilyInstance>> dictionary = rectangleConnTerminalCheck.GroupDuctsAndTerminals(listDuct2, list, ref list3, false);
							int num3 = list3.Count;
							foreach (KeyValuePair<Duct, List<FamilyInstance>> keyValuePair2 in dictionary)
							{
								int num4 = hyductConnectUtil.BatchConnTerminalByJoint(keyValuePair2.Key, keyValuePair2.Value, terminalConnType2, alignType2);
								num3 += keyValuePair2.Value.Count - num4;
							}
							if (num3 > 0)
							{
								YJKMessageBox.Information(string.Format("存在{0}个风口不符合插管连接条件，请手动调整或删除。", num3));
							}
						}
						break;
					}
					}
					RevitVersionFuncs.ClearSelection(this.sel);
					this.doc.Regenerate();
					transaction.Commit();
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					if (batchConnTerminalForm.BeAutoCancel)
					{
						batchConnTerminalForm.BeAutoCancel = false;
						continue;
					}
					batchConnTerminalForm.Close();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception ex)
				{
					YJKMessageBox.Error(ex.Message);
					batchConnTerminalForm.Close();
					result = Autodesk.Revit.UI.Result.Failed;
				}
				finally
				{
					if (transaction != null)
					{
						TransactionStatus status = transaction.GetStatus();
						if ((int)status == 1)
						{
							transaction.Commit();
						}
						else if ((int)status == 5)
						{
							transaction.RollBack();
						}
					}
				}
				break;
			}
			return result;
		}

		private UIApplication uiApp;

		private Document doc;

		private Selection sel;

		public static YJKWindowHandle hWndRevit;
	}
}
